package com.example.service.impl;

import com.example.entity.User;
import com.example.exception.BizException;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.example.vo.Result;
import com.example.vo.Result1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import static com.example.exception.SystemErrorType.BIZ;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Result1 findAll() {

        Result1 result1 = new Result1();

        Random rand=new Random();
        int n=rand.nextInt(3);//返回值在范围[0,3) 即[0,2]
        if (1==n) {
            result1.setStatus(500);
        }else {
            result1.setStatus(200);
        }

        return result1;
    }

    @Override
    public void fileExtraction(HttpServletResponse response, String type) {
        List<User> list =userMapper.select(type);
        //拼接字符串
        StringBuffer text = new StringBuffer();

        text.append("主键");
        text.append("|");
        text.append("序号");
        text.append("|");
        text.append("客户账号");
        text.append("|");
        text.append("金额");
        text.append("|");
        text.append("账户姓名");
        text.append("|");
        text.append("组");
        text.append("|");
        text.append("摘要");
        text.append("\r\n");//换行字符

        for(User item:list){
            text.append(item.getBusiId());
            text.append("|");
            text.append(item.getCusNum());
            text.append("|");
            text.append(item.getCusAccount());
            text.append("|");
            text.append(item.getTotalM());
            text.append("|");
            text.append(item.getCusName());
            text.append("|");
            text.append(item.getType());
            text.append("|");
            text.append(item.getRemark());
            text.append("\r\n");//换行字符
        }
        exportTxt(response,text.toString());
    }

    public void exportTxt(HttpServletResponse response,String text){
        response.setCharacterEncoding("utf-8");
        //设置响应的内容类型
        response.setContentType("text/plain");

        Date day = new Date();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String name = sdf.format(day);
        //设置文件的名称和格式
        response.addHeader("Content-Disposition","attachment;filename="
                + genAttachmentFileName( name, "JSON_FOR_UCC_")//设置名称格式，没有这个中文名称无法显示
                + ".txt");
        BufferedOutputStream buff = null;
        ServletOutputStream outStr = null;
        try {
            outStr = response.getOutputStream();
            buff = new BufferedOutputStream(outStr);
            buff.write(text.getBytes("UTF-8"));
            buff.flush();
            buff.close();
        } catch (Exception e) {
            //LOGGER.error("导出文件文件出错:{}",e);
        } finally {try {
            buff.close();
            outStr.close();
        } catch (Exception e) {
            //LOGGER.error("关闭流对象出错 e:{}",e);
        }
        }
    }

    public  String genAttachmentFileName(String cnName, String defaultName) {
        try {
            cnName = new String(cnName.getBytes("gb2312"), "ISO8859-1");
        } catch (Exception e) {
            cnName = defaultName;
        }
        return cnName;
    }

}
